/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "context_config.h"

static unordered_map<uint32_t, string> HashAlgoStrMap = {
    {SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_256, "TPM_ALG_SHA_256"},
    {SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_384, "TPM_ALG_SHA_384"},
    {SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_512, "TPM_ALG_SHA_512"}
};

static unordered_map<uint32_t, string> AsymAlgoStrMap = {
    {SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048, "TPM_ALG_RSAPSS_2048"},
    {SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072, "TPM_ALG_RSAPSS_3072"},
    {SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096, "TPM_ALG_RSAPSS_4096"}
};

static unordered_map<uint32_t, string> VersionMap = {
    {SPDM_MESSAGE_VERSION_10, "1.0"},
    {SPDM_MESSAGE_VERSION_11, "1.1"},
    {SPDM_MESSAGE_VERSION_12, "1.2"},
    {SPDM_MESSAGE_VERSION_13, "1.3"},
};

static unordered_map<string, unordered_map<uint32_t, string>> CapabilityMap = {
    {"HashAlgo", HashAlgoStrMap},
    {"AsymAlgo", AsymAlgoStrMap},
    {"Version",  VersionMap}
};

string GetCapabilityString(const string& cap_type, uint32_t cap)
{
    auto it = CapabilityMap.find(cap_type);
    if (it == CapabilityMap.end()) {
        debug_log(DLOG_ERROR, "get capability map failed, invalid cap type %s", cap_type.c_str());
        return "";
    }

    unordered_map<uint32_t, string>& map = it->second;

    auto it2 = map.find(cap);
    if (it2 == map.end()) {
        debug_log(DLOG_ERROR, "get capability string failed, invalid cap(%u) for %s", cap, cap_type.c_str());
        return "";
    }

    return it2->second;
}